import numpy as np
import random

class Game:
    def __init__(self):
        self.juzhen = np.array([[0]*4 for i in range(4)])

    def show(self):
        self.showjuzhen = [[0]*4 for i in range(4)]
        for i in range(4):
            for j in range(4):
                self.showjuzhen[i][j] = str(self.juzhen[i][j])
        print(self.showjuzhen[0][0]+' '*4+self.showjuzhen[0][1]+' '*4+self.showjuzhen[0][2]+' '*4+self.showjuzhen[0][3])
        print(self.showjuzhen[1][0]+' '*4+self.showjuzhen[1][1]+' '*4+self.showjuzhen[1][2]+' '*4+self.showjuzhen[1][3])
        print(self.showjuzhen[2][0]+' '*4+self.showjuzhen[2][1]+' '*4+self.showjuzhen[2][2]+' '*4+self.showjuzhen[2][3])
        print(self.showjuzhen[3][0]+' '*4+self.showjuzhen[3][1]+' '*4+self.showjuzhen[3][2]+' '*4+self.showjuzhen[3][3])

    def trans(self):
        self.juzhen = self.juzhen.transpose()

    def nizhuan(self):
        for i in range(4):
            self.juzhen[i] = self.juzhen[i][::-1]

    def right(self):
        for i in range(4):
                for j in range(3):
                    if self.juzhen[i][j] == self.juzhen[i][j+1]:
                        self.juzhen[i][j+1] += self.juzhen[i][j]
                        self.juzhen[i][j] = 0
                    if self.juzhen[i][j+1] == 0:
                        self.juzhen[i][j+1] = self.juzhen[i][j]
                        self.juzhen[i][j] = 0

    def left(self):
        self.nizhuan()
        self.right()
        self.nizhuan()

    def up(self):
        self.trans()
        self.nizhuan()
        self.right()
        self.nizhuan()
        self.trans()

    def down(self):
        self.trans()
        self.right()
        self.trans()

    def create(self):
        list1 = []
        for i in range(4):
            for j in range(4):
                if self.juzhen[i][j] == 0:
                    list1.append((i,j))
        if len(list1) != 0:
            (a,b) = random.choice(list1)
            self.juzhen[a][b] = random.choice([2,4])
        else:
            self.gameover()
            
    def gameover(self):
        self.show()
        print('You Lose!')

    def win(self):
        self.show()
        print('You Win!')

    def check(self):
        for i in range(4):
            for j in range(4):
                if self.juzhen[i][j] == 2048:
                    self.win()

    def move(self):
        print ("'W' for up, 'A' for left, 'S' for down, 'D' for right")
        m = input('Please Move:')
        if m == ('a' or 'A'):
            self.left()
        elif m == ('d' or 'D'):
            self.right()
        elif m == ('w' or 'W'):
            self.up()
        elif m == ('s' or 'S'):
            self.down()
        else:
            print ('Input Error, Try again!')
            self.move()
            
    def main(self):
        self.create()
        self.create()
        self.show()
        while True:
            self.move()
            self.check()
            self.create()
            self.show()
        
